﻿// // --------------------------------------------------------------------------------------------------------------------
// // <copyright file="GlobalSettingsPage.cs" company="Collaboris">
// //   Copyright Collaboris
// // </copyright>
// // <summary>
// //   
// // </summary>
// // --------------------------------------------------------------------------------------------------------------------

namespace DocRead.Selenium.Tests.DocRead.PageObjects.CA
{
    #region

    using System.Collections.Generic;
    using System.Collections.ObjectModel;

    using Collaboris.ShareTest.Core;
    using Collaboris.ShareTest.SharePoint2010.PageObjects;

    using OpenQA.Selenium;
    using OpenQA.Selenium.Remote;
    using OpenQA.Selenium.Support.UI;

    using global::DocRead.Selenium.Tests.DocRead.ControlObjects;

    #endregion

    /// <summary>
    /// 	Maps to the DocRead Global Settings page located in Central Admin
    /// </summary>
    public class GlobalSettingsPage : SPTPageObjectBase
    {
        private GlobalSettingsForm form;

        #region Constructors and Destructors

        public GlobalSettingsPage(RemoteWebDriver driver, string fullUrl)
            : base(driver, fullUrl)
        {
        }

        public GlobalSettingsPage(RemoteWebDriver driver, string fullUrl, string verifyElementOnLoad)
            : base(driver, fullUrl, verifyElementOnLoad)
        {
        }

        public GlobalSettingsPage(
            RemoteWebDriver driver, string fullUrl, string verifyElementOnLoad, string loginFileName)
            : base(driver, fullUrl, verifyElementOnLoad, loginFileName)
        {
        }

        #endregion


        #region Public Properties

        public GlobalSettingsForm Form
        {
            get
            {
                if (this.form == null)
                {
                    this.form = new GlobalSettingsForm(this.webDriver);
                    this.form.RefreshForm();
                }

                return this.form;
            }
            set
            {
                this.form = value;
            }
        }

        #endregion

        public override void RefreshPage()
        {
            base.RefreshPage();

            this.Form = null;
        }

        #region Public Methods

        public void Save()
        {
            // jobs management
            this.Form.SelectReadProcessingFrequency(this.Form.ReadershipJobFreq, this.Form.ReadershipInterval);
           
            this.Form.SelectAudProcessingFrequency(this.Form.AudiencesJobFreq, this.Form.AudiencesInterval);
           
            this.Form.SelectSpObjectsProcessingFrequency(this.Form.SharePointJobFreq, this.form.SharePointInterval);
           
            // Required days
            this.webDriver.SendKeysAndClearIfDiff(
                this.Form.DefaultRequiredDaysElement, this.Form.DefaultRequiredDays);

            this.webDriver.SendKeysAndClearIfDiff(
                this.Form.DefaultRecommendedDaysElement, this.Form.DefaultRecommendedDays);

            this.webDriver.SendKeysAndClearIfDiff(
                this.Form.MaxNumberThreadsElement, this.Form.MaxNumberThreads);

            if (this.Form.WebApplications.Count > 0)
            {
                SelectElement webApplicationSelector = new SelectElement(this.Form.WebApplicationSelectElement);
                foreach (string webApplication in this.Form.WebApplications)
                {
                    webApplicationSelector.SelectByValue(webApplication);
                }
            }

            this.PushOkThenRefreshIfNeeded();
        }

        #endregion

        #region Methods

        /// <summary>
        /// 	Only Refresh if the url changes
        /// </summary>
        private void PushOkThenRefreshIfNeeded()
        {
            string previousUrl = this.webDriver.Url;
            this.Form.OkButton.Click();

            if (this.webDriver.IsElementPresent(By.XPath("//button/span[text()='Proceed']/parent::button")))
            {
                this.webDriver.FindElementByXPath("//button/span[text()='Proceed']/parent::button").Click();
            }

            if (this.webDriver.Url == previousUrl)
            {
                this.Form.RefreshForm();
            }
        }

        #endregion

        public void ClickCancel()
        {
            this.Form.CancelButton.Click();
        }
    }

    public enum Frequency
    {
        Daily=2,
        Hourly=1,
        RunNowOnly=0
    }
}